package com.castle.fortress.admin.member.gropRelation.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.gropRelation.entity.MemberGroupRelationEntity;
import com.castle.fortress.admin.member.gropRelation.dto.MemberGroupRelationDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.member.gropRelation.service.MemberGroupRelationService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员分组关联关系表 api 控制器
 *
 * @author 
 * @since 2025-04-15
 */
@Api(tags="会员分组关联关系表api管理控制器")
@RestController
@RequestMapping("/api/member/gropRelation/memberGroupRelation")
public class ApiMemberGroupRelationController {
    @Autowired
    private MemberGroupRelationService memberGroupRelationService;


    /**
     * 会员分组关联关系表的分页展示
     * @param memberGroupRelationDto 会员分组关联关系表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员分组关联关系表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberGroupRelationDto>> pageMemberGroupRelation(MemberGroupRelationDto memberGroupRelationDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberGroupRelationDto> page = new Page(pageIndex, pageSize);

        IPage<MemberGroupRelationDto> pages = memberGroupRelationService.pageMemberGroupRelation(page, memberGroupRelationDto);
        return RespBody.data(pages);
    }

    /**
     * 会员分组关联关系表保存
     * @param memberGroupRelationDto 会员分组关联关系表实体类
     * @return
     */
    @ApiOperation("会员分组关联关系表保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberGroupRelation(@RequestBody MemberGroupRelationDto memberGroupRelationDto){
        if(memberGroupRelationDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberGroupRelationEntity memberGroupRelationEntity = ConvertUtil.transformObj(memberGroupRelationDto,MemberGroupRelationEntity.class);
        if(memberGroupRelationService.save(memberGroupRelationEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员分组关联关系表编辑
     * @param memberGroupRelationDto 会员分组关联关系表实体类
     * @return
     */
    @ApiOperation("会员分组关联关系表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberGroupRelation(@RequestBody MemberGroupRelationDto memberGroupRelationDto){
        if(memberGroupRelationDto == null || memberGroupRelationDto.getId() == null || memberGroupRelationDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberGroupRelationEntity memberGroupRelationEntity = ConvertUtil.transformObj(memberGroupRelationDto,MemberGroupRelationEntity.class);
        if(memberGroupRelationService.updateById(memberGroupRelationEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员分组关联关系表删除
     * @param ids 会员分组关联关系表id集合
     * @return
     */
    @ApiOperation("会员分组关联关系表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberGroupRelation(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberGroupRelationService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员分组关联关系表详情
     * @param id 会员分组关联关系表id
     * @return
     */
    @ApiOperation("会员分组关联关系表详情")
    @GetMapping("/info")
    public RespBody<MemberGroupRelationDto> infoMemberGroupRelation(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberGroupRelationEntity memberGroupRelationEntity = memberGroupRelationService.getById(id);
            MemberGroupRelationDto memberGroupRelationDto = ConvertUtil.transformObj(memberGroupRelationEntity,MemberGroupRelationDto.class);
        return RespBody.data(memberGroupRelationDto);
    }


}
